package jwcb.hire.admin.server.map.service.impl;

import jwcb.hire.admin.server.map.domain.dto.qq.GeoResultDto;
import jwcb.hire.admin.server.map.domain.dto.qq.LocationDTO;
import jwcb.hire.admin.server.map.domain.dto.qq.PoiListDTO;
import jwcb.hire.admin.server.map.domain.dto.qq.QQSuggestSearchDTO;
import jwcb.hire.admin.server.map.service.IMapProvider;
import jwcb.hire.common.domain.domain.ResultCode;
import jwcb.hire.common.domain.exception.ServiceException;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@RefreshScope
@Component
@Getter
@Setter
@Slf4j
@ConditionalOnProperty(value = "map.type",havingValue = "qqmap")
@Service
public class QQMapProviderImpl implements IMapProvider<QQSuggestSearchDTO> {


    /**
     * 调用腾讯位置服务的密钥
     */
    @Value("${qqmap.key}")
    private String qqmapKey;

    @Value("${qqmap.apiService}")
    private String apiService;


    /**
     * 关键系搜索地址信息路由
     */
    @Value("${qqmap.suggest_api_route}")
    private String suggestApiRoute;

    /**
     * 根据经纬度来获取地址信息的路由
     */
    @Value("${qqmap.decode_api_route}")
    private String decodeApiRoute;


    @Autowired
    private RestTemplate restTemplate;
    /**
     * 根据关键词搜索地点
     * @param qqSuggestSearchDTO 搜索条件
     * @return 搜索结果
     */
    @Override
    public PoiListDTO searchMapPlaceByRegion(QQSuggestSearchDTO qqSuggestSearchDTO) {
        // 拼接url
        String url = String.format(
                apiService + suggestApiRoute + "?key=%s&region=%s&region_fix=1&pageIndex=%d&pageSize=%d&keyword=%s"
                ,qqmapKey,qqSuggestSearchDTO.getId(),qqSuggestSearchDTO.getPageIndex(),qqSuggestSearchDTO.getPageSize(),qqSuggestSearchDTO.getKeyword()
        );
        // 执行请求
        ResponseEntity<PoiListDTO> response = restTemplate.getForEntity(url, PoiListDTO.class);
        if(!response.getStatusCode().is2xxSuccessful()) {
            log.error("获取关键词结果异常");
            throw new ServiceException(ResultCode.QQMAP_QUERY_FAILED);
        }
        return response.getBody();
    }


    /**
     * 根据经纬度获取区域信息
     * @param locationDTO 经纬度据悉你
     * @return 区域信息
     */
    @Override
    public GeoResultDto getMapDistrictByLonLat(LocationDTO locationDTO) {
        // 拼接url
        String url = String.format(
                apiService + decodeApiRoute + "?key=%s&location=%s"
                ,qqmapKey,locationDTO.formatInfo()
        );
        // 执行请求
        ResponseEntity<GeoResultDto> response = restTemplate.getForEntity(url, GeoResultDto.class);
        if(!response.getStatusCode().is2xxSuccessful()) {
            log.error("根据经纬度获取区域信息查新结果异常");
            throw new ServiceException(ResultCode.QQMAP_QUERY_FAILED);
        }
        return response.getBody();
    }
}
